﻿Imports System.Web.HttpUtility

Public Class MemorizedSongsList
  Public Class MemorizedItem
    Private _artist As String = String.Empty
    Private _title As String = String.Empty
    Private _station As String = String.Empty
    Private _time As Date = DateTime.MinValue

    Public Property Artist() As String
      Get
        Return Me._artist
      End Get
      Set(ByVal value As String)
        Me._artist = value
      End Set
    End Property

    Public Property Title() As String
      Get
        Return Me._title
      End Get
      Set(ByVal value As String)
        Me._title = value
      End Set
    End Property

    Public Property Station() As String
      Get
        Return Me._station
      End Get
      Set(ByVal value As String)
        Me._station = value
      End Set
    End Property

    Public Property Time() As Date
      Get
        Return Me._time
      End Get
      Set(ByVal value As Date)
        Me._time = value
      End Set
    End Property
  End Class

  'TODO: Load all memorized songs
  'TODO: Save all memorized songs

  Private _memorizedItems As New List(Of MemorizedItem)
  Private _filePath As String

  Public Sub New(ByVal memFilePath As String)
    Me._filePath = memFilePath
  End Sub

  ''' <summary>
  ''' Returns 'true' on success, 'false' on failure
  ''' </summary>
  Public Function AppendMemorizedSong(ByVal song As MemorizedItem) As Boolean
    Try
      If song IsNot Nothing Then
        Dim fs As New IO.FileStream(Me._filePath, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Read)

        Try
          Dim sw As New IO.StreamWriter(fs)
          sw.WriteLine("Artist: " & song.Artist)
          sw.WriteLine("Title: " & song.Title)
          sw.WriteLine("Station: " & song.Station)
          sw.WriteLine("Time: " & song.Time)
          sw.WriteLine()
          sw.Flush()
          sw.Close()

          Return True
        Catch
          Throw
        Finally
          Try : fs.Close() : Catch : End Try
        End Try
      Else
        Throw New Exception("Song data is NULL.")
      End If
    Catch ex As Exception
      ErrorLog.WriteToErrorLog("MemorizedSongsList :: AppendMemorizedSong :: " & ex.ToString)
    End Try

    Return False
  End Function

  ''' <summary>
  ''' Returns 'true' when a dupe is found and 'false' for 'no dupe' but also for error (which will be caught on subsequent operation, like append
  ''' </summary>
  Public Function CheckForDupe(ByVal song As MemorizedItem) As Boolean
    Try
      If IO.File.Exists(Me._filePath) Then
        If song IsNot Nothing Then
          Dim fs As New IO.FileStream(Me._filePath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

          Try
            Dim sr As New IO.StreamReader(fs)

            Dim curArtist As String = song.Artist.Trim
            Dim curTitle As String = song.Title.Trim
            Dim line As String = String.Empty
            Dim lastArtist As String = String.Empty
            Dim lastTitle As String = String.Empty
            Dim foundArtist As Boolean = False
            Dim foundTitle As Boolean = False

            While Not sr.EndOfStream
              line = sr.ReadLine().Trim

              If line.Length > 0 Then
                If String.Compare(Left(line, 7), "Artist:", True) = 0 Then
                  lastArtist = Mid(line, 8).Trim
                  foundArtist = True
                ElseIf String.Compare(Left(line, 6), "Title:", True) = 0 Then
                  lastTitle = Mid(line, 7).Trim
                  foundTitle = True
                End If

                If foundArtist And foundTitle Then
                  If lastArtist.Length > 0 AndAlso lastTitle.Length <= 0 Then
                    ' Title is the entry we use for single title entries, not artist, so flip it, as this was a mistake
                    lastTitle = lastArtist
                    lastArtist = String.Empty
                  End If

                  If String.Compare(lastArtist, curArtist, True) = 0 AndAlso String.Compare(lastTitle, curTitle, True) = 0 Then
                    Return True
                  End If

                  lastArtist = String.Empty
                  lastTitle = String.Empty
                  foundArtist = False
                  foundTitle = False
                End If
              Else ' If the line is blank, this separates entries, so reset the flags.  This helps if artist/title gets out of sync
                lastArtist = String.Empty
                lastTitle = String.Empty
                foundArtist = False
                foundTitle = False
              End If
            End While
          Catch
            Throw
          Finally
            Try : fs.Close() : Catch : End Try
          End Try
        Else
          Throw New Exception("Song data is NULL.")
        End If
      End If
    Catch ex As Exception
      ErrorLog.WriteToErrorLog("MemorizedSongsList :: CheckForDupe :: " & ex.ToString)
    End Try

    Return False
  End Function

End Class
